/**
 * created by gw
 * 2018-10-22
 * 将老的模板数据替换为新的模板数据的方法
 */
import data2 from "./productionData";
// import data2 from "./preData";
import speData from "./specialData";
import LinkedList from "../../helper/LinkedList";
// console.log("data--->", JSON.parse(data));
let template = {

}
let d = JSON.parse(data2);
// console.log("data--->", d);
for (let page in d) {
    //这个是为了转换specialTemplate
    if (page === "config") {
        template.config = d.config;
        continue;
    }
    if (page === "config" || Array.isArray(d[page])) continue;
    for (let part in d[page]) {
        if (!template[page]) {
            template[page] = {
                config: {},
                header: new LinkedList(),
                content: new LinkedList(),
                footer: new LinkedList()
            };
        }
        if (part === "config") {
            template[page].config = d[page].config;
            continue;
        }
        // console.log('2222222222', page, part, d[page][part]);
        // if (Object.prototype.toString.call(d[page][part]) === "[object Object]") continue;
        if (!Array.isArray(d[page][part])) continue;
        //先把undefined剔除
        d[page][part].sort((m, n) => m.index - n.index);
        d[page][part].forEach((item1, index) => {
            // let obj = d[page][part].find(item2 => item2.index === index);
            // let obj = item1;
            if (item1) {
                let obj = {
                    ...item1,
                    componentName: item1.type
                }
                delete obj.type;
                template[page][part].push(obj);
            }
        });
    }
    template[page].main = JSON.parse(JSON.stringify(template[page].content));
    delete template[page].content;
}
let result = JSON.stringify(template);
// console.log("获得新模板的数据", result);

//处理specialTemplate
let sd = JSON.parse(speData);
let speTemplate = {};
//先拿到config
speTemplate.config = sd.config;
//定义一个正则表达式，筛选出自定义页面
let reg = /^s[0-9]+$/;
for (let page in template) {
    if (reg.test(page)) {
        speTemplate[page] = template[page];
    }
}
let speResult = JSON.stringify(speTemplate);
console.log("获得新的spe模板的数据", speResult);
console.log("speTemplate", speTemplate);

// window.testStr = result;

